package com.sgb.kjwl.view.ui.fragment.home;

import android.content.Context;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentOptionBinding;
import com.sgb.kjwl.di.home.DaggerHomeFragComponent;
import com.sgb.kjwl.di.home.HomeFragModule;
import com.sgb.kjwl.model.entity.MaterialListEntity;
import com.sgb.kjwl.view.ui.activity.self.shop.ShopGoodsDetailActivity;
import com.sgb.kjwl.view.ui.adapter.home.OptionAdapter;
import com.sgb.kjwl.viewmodel.home.OptionFragModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;

import java.util.List;

import javax.inject.Inject;

/**
 * Description:
 * Author zhengkewen
 */
public class OptionFragment extends AppFragment {
    public static final String TAG = "";
    public static final String KEY_TYPE = "type";

    private FragmentOptionBinding binding;
    private String type = "";
    private boolean isCreate = false;
    @Inject
    OptionFragModel viewModel;
    private OptionAdapter optionAdapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isCreate = true;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (!isCreate) {
            return;
        }
        if (isVisibleToUser) {
            //     viewModel.getReceptionListViewModel(keyword, type);
        }
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerHomeFragComponent.builder()
                .appComponent(appComponent)
                .homeFragModule(new HomeFragModule(this))
                .build()
                .inject(this);
    }

    public static OptionFragment newInstance(String type) {
        OptionFragment fragment = new OptionFragment();
        Bundle bundle = new Bundle();
        bundle.putString(KEY_TYPE, type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_option, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        type = (String) getArguments().get(KEY_TYPE);
        initView();
        viewModel.getMaterialListModel(type, "");
    }

    private void initView() {
        if (optionAdapter == null) {
            optionAdapter = new OptionAdapter(getActivity(), null);
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.rlvOption.setLayoutManager(layoutManager);
        binding.rlvOption.addItemDecoration(new RecyclerCustomDecoration(0,
                0,
                0,
                getActivity().getResources().getDimension(R.dimen.dp1), getActivity().getResources().getColor(R.color.color_line)));
        binding.rlvOption.setAdapter(optionAdapter);
        optionAdapter.setOnItemClick(new AdapterOnItemClick<MaterialListEntity>() {
            @Override
            public void onItemClick(MaterialListEntity materialListEntity, int position) {
                ShopGoodsDetailActivity.start(getActivity(), materialListEntity. getId(),"", materialListEntity.getSkuid());
//                String link = AppConstant.MATERIAL_MATERIAL + materialListEntity.getId();
//                EventBus.getDefault().postSticky(WebEvent.make().setMessageTag(EventConstant.EVENT_WEBVIEW).setMessageData(link));
//                WebActivity.start(getActivity());
            }
        });
    }

    public void showOptionView(List<MaterialListEntity> datas) {
        if(datas.isEmpty()){
            binding.empty.setVisibility(View.VISIBLE);
            binding.rlvOption.setVisibility(View.GONE);
        }
        optionAdapter.setDatas(datas);
    }

}
